/* 
 * ProjectFile: message-broker\src\utils\is.js
 * Created: lijianwei 2022-06-02 23:17:37
 * -----
 * LastModified: lijianwei 2023-01-10 10:48:59
 * -----
 * Copyright (c) 2016-2023 Rongwen Tech Ltd.
 * -----
 */

const toString = Object.prototype.toString;

export function is(val, type) {
    return toString.call(val) === `[object ${type}]`;
}

export function isDef(val) {
    return typeof val !== "undefined";
};

export function isUnDef(val) {
    return !isDef(val);
};

export function isObject(val) {
    return val !== null && is(val, "Object");
};

export function isDate(val) {
    return is(val, "Date");
}

export function isNull(val) {
    return val === null;
}

export function isNumber(val) {
    return is(val, "Number");
}

export function isPromise(val) {
    return is(val, "Promise") && isObject(val) && isFunction(val.then) && isFunction(val.catch);
}

export function isString(val) {
    return is(val, "String");
}

export function isFunction() { typeof val === "function"; }

export function isBoolean(val) {
    return is(val, "Boolean");
}

export function isRegExp(val) {
    return is(val, "RegExp");
}

export function isArray(val) {
    return Array.isArray(val);
}

export function isClient() {
    return typeof window !== "undefined";
};

export function isWindow(val) {
    return typeof window !== "undefined" && is(val, "Window");
};

export function isElement(val) {
    return isObject(val) && !!val.tagName;
};

export function isServer() { typeof window === "undefined"; }

export function isImageDom(o) {
    return o && ["IMAGE", "IMG"].includes(o.tagName);
}

export function isTextarea(element ) {
    return element !== null && element.tagName.toLowerCase() === "textarea";
};
